package com.xxkj.drugtraceability.ui.main

import android.content.Intent
import android.os.SystemClock
import android.view.KeyEvent
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.xxhis.refactor.eventbus.MessageCode
import com.xxkj.drugtraceability.App
import com.xxkj.drugtraceability.R
import com.xxkj.drugtraceability.base.BaseVMActivity
import com.xxkj.drugtraceability.base.Constants
import com.xxkj.drugtraceability.bean.EventMessage
import com.xxkj.drugtraceability.databinding.ActivityMainBinding
import com.xxkj.drugtraceability.dialog.BlueDialog
import com.xxkj.drugtraceability.dialog.YellowDialog
import com.xxkj.drugtraceability.ext.putSpValue
import com.xxkj.drugtraceability.ext.startKtxActivity
import com.xxkj.drugtraceability.lifecycle.KtxManager
import com.xxkj.drugtraceability.ui.login.LoginActivity
import com.xxkj.drugtraceability.ui.start.StartActivity
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.koin.androidx.viewmodel.ext.android.viewModel
import kotlin.system.exitProcess

/**
 * author: zhoujingjin
 * created on: 2024/9/10 8:45
 * description:首页
 */
class MainActivity:BaseVMActivity() {
    private val binding by binding<ActivityMainBinding>(R.layout.activity_main)
    private val viewModel by viewModel<MainViewModel>()
    private lateinit var mainAdapter: MainAdapter
    override fun initView() {
        binding.apply {
            mainAdapter = MainAdapter()
            recyclerView.layoutManager = LinearLayoutManager(this@MainActivity)
            recyclerView.adapter = mainAdapter
            swipe.setColorSchemeColors(resources.getColor(R.color.toolbar_background))
            swipe.setOnRefreshListener {
                viewModel.getRouters()
            }
        }
    }

    override fun initData() {
        binding.swipe.post {
            binding.swipe.isRefreshing = true
            viewModel.getRouters()
        }
    }
    override fun startObserve() {
        viewModel.uiState.observe(this, Observer {
            binding.swipe.isRefreshing = it.isRefresh
            if (!it.showError.isNullOrEmpty()){
                YellowDialog(this, it.showError!!, 1) {}
            }
        })
        viewModel.dataList.observe(this, Observer {
            mainAdapter.setList(it)
        })
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onGetMessage(message: EventMessage?) {
        if (message?.messageCode== MessageCode.NEED_LOGIN){
            KtxManager.currentActivity?.let {
                it as BaseVMActivity
                it.dismissMyDialog()
                YellowDialog(it,"登录超时，请重新登录",3){
                    App.token = ""
                    KtxManager.finishAllActivity()
                    startKtxActivity<StartActivity>()
                }
            }
        }
    }
    override fun onStart() {
        super.onStart()
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }


    // 在Activity中使用
    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            val currentTimeMillis = SystemClock.uptimeMillis()
            if (currentTimeMillis - lastPressTime < 500) {
                putSpValue(Constants.TOKEN,"")
                finish() // 关闭当前Activity
                exitProcess(0) // 完全退出应用
            } else {
                lastPressTime = currentTimeMillis
                Toast.makeText(this, "再按一次退出应用", Toast.LENGTH_SHORT).show()
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    private var lastPressTime: Long = 0
}